//
//  AlbumDetailInfoCell.swift
//  GhostWorld
//
//  Created by Kam on 2018/4/6.
//  Copyright © 2018 LKGood. All rights reserved.
//

import UIKit
import Alamofire

protocol AlbumDetailInfoCellDelegate {
  func buyAlbum()
}

class AlbumDetailInfoCell: UITableViewCell {

  // MARK: - IBOutlets
  
  @IBOutlet private weak var coverImageView: CoverImageView!
  @IBOutlet private weak var nameLabel: UILabel!
  @IBOutlet private weak var writerNameLabel: UILabel!
  @IBOutlet private weak var readerNameLabel: UILabel!
  @IBOutlet private weak var purchaseView: UIView!
  @IBOutlet private weak var memberView: UIView!
  @IBOutlet private weak var buyButton: UIButton!
  @IBOutlet private weak var priceLabel: UILabel!
  @IBOutlet private weak var descriptionLabel: UILabel!
  @IBOutlet private weak var purchasedView: UIView!
  @IBOutlet private weak var purchasedLabel: UILabel!
  
  // MARK: - Private vars
  
  
  
  // MARK: - Private methods
  
  
  
  // MARK: - Public vars
  
  var delegate: AlbumDetailInfoCellDelegate?
  
  // MARK: - Lifecycle
  
  override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
    
    purchaseView.isHidden = true
  }
  
  override func setSelected(_ selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)
    
    // Configure the view for the selected state
  }
  
  // MARK: - IBActions
  
  @IBAction func buyAlbum() {
    delegate?.buyAlbum()
  }
  
  // MARK: - Public methods
  
  func prepare(album: Album, isUserPurchased: Bool, purchaseDateString: String?, isPurchasedByIAP: Bool) {
    nameLabel.text = album.name
    if let count = album.writers?.count, count > 0 {
      writerNameLabel.text = album.writers!.map{ ($0 as! Writer).name! }.joined(separator: ",")
    } else {
      writerNameLabel.text = "未知"
    }
    if let count = album.readers?.count, count > 0 {
      readerNameLabel.text = album.readers!.map{ ($0 as! Reader).name! }.joined(separator: ",")
    } else {
      readerNameLabel.text = "未知"
    }
    if let url = album.cover.getURL(size: .thumb) {
      coverImageView.af_setImage(withURL: url)
    }
    descriptionLabel.text = album.albumDescription ?? "--"
    
    if album.isVipContent {
      memberView.isHidden = false
      purchasedView.isHidden = true
      purchaseView.isHidden = true
    } else {
      memberView.isHidden = true
      if album.isFree {
        purchasedView.isHidden = true
        purchaseView.isHidden = true
      } else {
        if isUserPurchased {
          purchasedView.isHidden = false
          purchaseView.isHidden = true
          if let dateString = purchaseDateString,
            let date = Date(fromString: dateString, format: .dateTime) {
            purchasedLabel.text = "已于\(date.toString(format: .dateWithChinese))\(isPurchasedByIAP ? "购买" : "兑换")"
          } else {
            purchasedLabel.text = "已\(isPurchasedByIAP ? "购买" : "兑换")"
          }
        } else {
          purchasedView.isHidden = true
          purchaseView.isHidden = false
          priceLabel.text = "\(album.iapPrice) ×"
        }
      }
    }
  }
}
